/**
 * some methods are often used. 
 * 2006.11.1
 */
package qy.course.cse494.util;

import java.util.Collection;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * @author yan qi
 *
 */
public final class UtilCenter
{
	public static double SquareDistanceOfPoints(double x1, double y1, double x2, double y2)
	{
		return (y1-y2)*(y1-y2) + (x1-x2)*(x1-x2);
	}
	
	public static double SquareDistance2Line(double x1, double y1, double x2, double y2, double px, double py)
	{
		double x_ft, y_ft, x_fp, y_fp;
		double t, d_ft;
		double r;
		
		x_ft = x1 - x2; 
		y_ft = y1 - y2;
		x_fp = x1 - px; 
		y_fp = y1 - py;
		t = x_ft*x_fp + y_ft*y_fp;
	
		d_ft = SquareDistanceOfPoints(x1, y1, x2, y2);
		r = x_ft*y_fp - x_fp*y_ft;
		r *= r; 
		r /= d_ft;
		return r;
	}
	
	
	public static Vector<String> ParseMultiInput(final String pInputString, final String pDelimit)
	{
		Vector<String> ret = new Vector<String>();
		
		try {
			Pattern Regex = Pattern.compile(pDelimit,Pattern.CANON_EQ);
			Matcher RegexMatcher = Regex.matcher(pInputString);
			while (RegexMatcher.find()) 
			{
				if(!RegexMatcher.group().trim().equals(""))
				{
					ret.add(RegexMatcher.group());
				}
			} 
		} catch (PatternSyntaxException ex) 
		{
			ex.printStackTrace();
		}
//		System.out.println("The list of string is:"+ret.toString());
		return ret;
	}
	
	public static Vector<Double> ParseMultiInputDouble(final String pInputString, final String pDelimit)
	{
		Vector<String> string_list = ParseMultiInput(pInputString, pDelimit);
		Vector<Double> ret = new Vector<Double>();
		for(String cur_str : string_list)
		{
			try
			{
				ret.add(Double.parseDouble(cur_str));
			} catch(NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		return ret;
	}
	
	public static Vector<Integer> ParseMultiInputInteger(final String pInputString, final String pDelimit)
	{
		Vector<String> string_list = ParseMultiInput(pInputString, pDelimit);
		Vector<Integer> ret = new Vector<Integer>();
		for(String cur_str : string_list)
		{
			try
			{
				ret.add(Integer.parseInt(cur_str));
			} catch(NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
		
//		System.out.println("The list of integer is:"+ret.toString());
		return ret;
	}
	
	public static boolean ExtractStringElements(final String pInputString, final String pDelimit, Collection<String> ret)
	{
		try {
			Pattern Regex = Pattern.compile(pDelimit,Pattern.CANON_EQ);
			Matcher RegexMatcher = Regex.matcher(pInputString);
			while (RegexMatcher.find()) 
			{
				if(!RegexMatcher.group().trim().equals(""))
				{
					ret.add(RegexMatcher.group());
				}
			} 
		} catch (PatternSyntaxException ex) 
		{
			ex.printStackTrace();
		}
//		System.out.println("The list of string is:"+ret.toString());
		return true;
	}
	
	public static boolean ExtractIntegerElements(final String pInputString, final String pDelimit, Collection<Integer> ret)
	{
		Vector<String> string_list = ParseMultiInput(pInputString, pDelimit);
		//System.out.println(string_list.size());
		for(String cur_str : string_list)
		{
			try
			{
				ret.add(Integer.parseInt(cur_str));
			} catch(NumberFormatException e)
			{
				e.printStackTrace();
			}
		}
//		System.out.println("The list of integer is:"+ret.toString());
		return true;
	}

	static public String ObjectArrayToStringList(final Object[] objArray, final String delimit)
	{
		StringBuffer val_list = new StringBuffer();
		int number_of_values = objArray.length;
		for(int i=0; i<number_of_values; ++i)
		{
			val_list.append((String)objArray[i]);
			if(number_of_values > 1 && i < number_of_values-1 )
			{
				val_list.append(delimit);
			}
		}
		return val_list.toString();
	}
	
	/**
	 * @description check if the string "input" can be converted into one integer.
	 * @param input
	 * @return
	 */
	static public boolean IsInteger(final String input)
	{
		try{
			Integer.parseInt(input);
			return true; //Did not throw, must be a number
		}catch(NumberFormatException err){
			return false; //Threw, So is not a number
		}
	}
	
}
